package org.pp.controller;


import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.pp.core.PageBean;
import org.pp.core.Result;
import org.pp.dto.PageDto;
import org.pp.miniapp.dto.CartAdd;
import org.pp.miniapp.dto.CartSet;
import org.pp.service.IPpCartService;
import org.pp.vo.MiniappCartVo;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
@Slf4j
public class PpCartController {

    @Autowired
    IPpCartService cartService;

    @PostMapping("/sku/add")
    @Operation(summary = "添加商品到购物车")
    public Result<Long> add(@RequestBody @Validated CartAdd dto){
        return Result.ok(cartService.add(dto));
    }

    @PostMapping("/sku/set")
    @Operation(summary = "设置购物车条目数")
    public Result<Void> skuSet(@RequestBody @Validated CartSet dto){
        cartService.skuSet(dto);
        return Result.ok();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除购物车商品条目")
    public Result<Void> delete(@RequestParam(required = true) Long cartId){
        cartService.delete(cartId);
        return Result.ok();
    }

    @GetMapping("/count")
    @Operation(summary = "购物车条目总和")
    public Result<Long> count(){
        return Result.ok(cartService.getCount());
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询购物车条目")
    public Result<PageBean<MiniappCartVo>> pageList(@ParameterObject PageDto dto){
        return Result.ok(cartService.pageList(dto));
    }
}
